<style>
	div.filterGroupRules {
		margin-top:2px;
	}
	fieldset {
		width: 800px;
		padding: 5px 5px;
	}
</style>
<script type="text/javascript">
	var tableNameValue = '${tableName?default("")}';
	function selectTableName(value) {
		printLog(value);
		tableNameValue = value.value;
// 		$('#selectColumn').combobox('reload', 'resource.do?getSelectFieldsByTableName&tableName='+value.value);   
	}
	
	function saveResoucePermissionInfo() {
    	printLog($("#resourcePermissionForm").serialize());
    	$.post("resource.do?saveResoucePermissionInfo",{formData:$("#resourcePermissionForm").serialize()}, function(result) {
               if(result.success){
            	  showMsg('保存成功.');
            	  reloadResource();
               }else {
            	  showMsg('保存失败.');
               }
        }, "JSON");
	}
// 	function loadColumns() {
// 		$('#selectColumn').combobox({   
// 		    url:'',   
// 		    valueField:'name',   
// 		    textField:'value'  
// 		});  
// 	}
	
// 	loadColumns();
	var j=${groupIndex?default()};
	//添加分组
	function addFilterGroup() {
		++j;
		++i;
		$('#resourcePermissionTblTd').append("<fieldset id=\"fsGroup"+j+"\" ><input type='hidden' name='splitgp' value='gp' /><legend>分组"+j+"<input id=\"\" name=\"filterGroupCheckBox\" type=\"checkbox\" value='"+j+"'/>操作符    <input style=\"width:159px;\" class=\"easyui-combobox\"  name=\"groupOption\" data-options=\"url:'common.do?loadComboBox&key=RULE_OPTION&value=and',valueField:'value',textField:'name'\" />    <a href=\"#\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-add'\" onclick=\"addFilterGroupRule("+j+");\">新增规则</a> <a href=\"#\" class=\"easyui-linkbutton\" data-options=\"iconCls:'icon-remove'\" onclick=\"deleteFilterGroupRule("+j+");\">删除规则</a></legend><div id=\"filterGroupRules"+j+"\"><div class=\"filterGroupRules\" id='filterGroupRuleDiv"+j+i+"'>规则"+i+"<input type='hidden' name='splitrl' value='rl' /><input id=\"filterRuleCheckbox\" name=\"filterGroupRuleCheckBox\" type=\"checkbox\" value='"+i+"'/>    <input id=\"ruleField"+i+"\" style=\"width:159px;\" class=\"easyui-combobox\"  name=\"ruleField\" data-options=\"url:'resource.do?getSelectFieldsByTableName&tableName="+tableNameValue+"',method:'get',valueField:'value',textField:'name',onSelect:function(v) {loadSelectedList("+i+")}\">    &nbsp;&nbsp;<input id=\"ruleType"+i+"\" style=\"width:159px;\" class=\"easyui-combobox\"  name=\"ruleType\" data-options=\"url:'common.do?loadComboBox&key=RULE_TYPE',onSelect:function(v) {loadSelectedList("+i+")},valueField:'value',textField:'name'\" />    &nbsp;&nbsp;<input id=\"selectedRuleValue"+i+"\" style=\"width:159px;\" class=\"easyui-combotree\" /><input id=\"ruleValueHidden"+i+"\" name=\"ruleValue\" type=\"hidden\"/></div></div></fieldset>");
		$.parser.parse($('#fsGroup'+j).parent());
	}
	//删除分组
	function deleteFilterGroup() {
		var hasCheck = false;
		var id_array=new Array();  
		$('input[name="filterGroupCheckBox"]:checked').each(function(){  
			hasCheck = true;
// 		    alert($(this).val());
		    id_array.push($(this).val());//向数组中添加元素  
		});
		printLog(id_array);
		if(id_array.length>0) {
			$.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
	            	printLog(id_array.join(','));
	            	for(var k=0;k<id_array.length;k++){ 
// 	            		printLog(id_array[k]);
	            		delGroupHtml(id_array[k]);
	            	} 
	            }
	        });
		}else {
			showMsg('请选择删除组');
		}
	}
	
	function delGroupHtml(id) {
		$('#fsGroup'+id).remove();
	}
	var i=${ruleIndex};
	//添加分组规则
	function addFilterGroupRule(index) {
		++i
		$('#filterGroupRules'+index).append("<div class=\"filterGroupRules\" id='filterGroupRuleDiv"+index+i+"'>规则"+i+"<input type='hidden' name='splitrl' value='rl' /><input id=\"filterRuleCheckbox\" name=\"filterGroupRuleCheckBox\" type=\"checkbox\" value='"+i+"'/>    <input id=\"ruleField"+i+"\" style=\"width:159px;\" class=\"easyui-combobox\"  name=\"ruleField\" data-options=\"url:'resource.do?getSelectFieldsByTableName&tableName="+tableNameValue+"',method:'get',valueField:'value',textField:'name',onSelect:function(v) {loadSelectedList("+i+")}\">    &nbsp;&nbsp;<input id=\"ruleType"+i+"\" style=\"width:159px;\" class=\"easyui-combobox\"  name=\"ruleType\" data-options=\"url:'common.do?loadComboBox&key=RULE_TYPE',onSelect:function(v) {loadSelectedList("+i+")},valueField:'value',textField:'name'\" />    &nbsp;&nbsp;<input id=\"selectedRuleValue"+i+"\" style=\"width:159px;\" class=\"easyui-combotree\" /><input id=\"ruleValueHidden"+i+"\" name=\"ruleValue\" type=\"hidden\"/>    &nbsp;&nbsp;<input style=\"width:159px;\" class=\"easyui-combobox\"  name=\"ruleOption\" data-options=\"url:'common.do?loadComboBox&key=RULE_OPTION&value=and',valueField:'value',textField:'name'\" /></div>");
		$.parser.parse($('#filterGroupRuleDiv'+index+i));
	}
	
	function deleteFilterGroupRule(index) {
		$('#filterGroupRules'+index)
		var ids=new Array();  
		$('#filterGroupRules'+index+' input[name="filterGroupRuleCheckBox"]:checked').each(function(){  
			hasCheck = true;
// 		    alert($(this).val());
		    ids.push($(this).val());//向数组中添加元素  
		});
		printLog(ids);
		if(ids.length>0) {
			$.messager.confirm('询问','你确认要删除吗?',function(r){
	            if (r){
// 	            	printLog(ids.join(','));
	            	for(var n=0;n<ids.length;n++){ 
// 	            		printLog(id_array[k]);
	            		delRuleHtml(index,ids[n]);
	            	} 
	            }
	        });
		}else {
			showMsg('请选择删除规则');
		}
	}
	
	function delRuleHtml(groupIndex,id) {
		$('#filterGroupRuleDiv'+groupIndex+''+id).remove();
	}
	  
	function loadSelectedList(i) {
// 		if(value=='in') {
// 			$('#selectedRuleValue'+i).attr("multiple","multiple");
// 		}else {
// 			$('#selectedRuleValue'+i).removeAttr("multiple");
// 		}
		printLog(i);
// 		printLog(value);
		var fieldValue = $('#ruleField'+i).combobox('getValue');
		var typeValue =  $('#ruleType'+i).combobox('getValue');
		printLog(fieldValue);
		printLog(typeValue);
		if(isNotEmpty(fieldValue)&&isNotEmpty(typeValue)) {
			var isMultiple = (typeValue=='in')
			printLog(isMultiple);
			$('#selectedRuleValue'+i).combotree({
				url: 'resource.do?loadRuleValues&fieldValue='+fieldValue,
				multiple:isMultiple,
				onChange:function(a,b) {printLog(a);printLog($('#selectedRuleValue'+i).combotree('getValue'));printLog($('#selectedRuleValue'+i).combotree('getValues'));$('#ruleValueHidden'+i).val($('#selectedRuleValue'+i).combotree('getValues'));},
			});
			
		}
		
// 		$('#selectedRuleValue'+i).combobox('reload', 'resource.do?getSelectFieldsByTableName&tableName='+value.value);   
// 		$('#selectedRuleValue'+i).html("<option value=\"1\">超级管理员</option><option value=\"2\">普通管理员</option>");
// 		$.parser.parse($('#selectedRuleValue'+i).parent());
	}
	
	
	function addGroup() {
// 		$('#resourcePermissionTblTd').append('<select id="op" class="easyui-combobox" name="ruleOption" style="width:159px;"><option value="in">包括在...</option><option value="eq">相等</option></select>	');
		$('#resourcePermissionTblTd').append('<input id="op" class="easyui-combobox"  name="language" data-options="url:\'resource.do?getSelectFieldsByTableName\',method:\'get\',valueField:\'value\',textField:\'name\',panelHeight:\'auto\',multiple:true">');
		
		$.parser.parse($('#op').parent());
	}
	
</script> 
<form id="resourcePermissionForm">
<table id="resourcePermissionTbl" cellpadding="5">
	<input name="id" type="hidden" value="${(resourceItem.id)!}" />
	<tr>
		<td>资源类型</td>
		<td>
			<input name="type"  class="easyui-validatebox" data-options="required: true" value="${(resourceItem.type)!}" />
		</td>
	</tr>
	<tr>
		<td>资源key</td>
		<td>
			<@eu.comboBox url="resource.do?loadTableNames&tableName=${tableName!}" name="key" onSelect="selectTableName(value)"/>
		</td>
	</tr>
	<tr>
		<td>
			分组操作
		</td>
		<td>
			<@eu.linkbutton showName="新增分组" iconCls="icon-add" onclick="addFilterGroup()"/>	
			<@eu.linkbutton showName="删除分组" iconCls="icon-remove" onclick="deleteFilterGroup()"/>	
			
<!-- 			<@eu.linkbutton showName="测试分组" shiro="sys:resource:delete" iconCls="icon-remove" onclick="addGroup()"/>			 -->
			
		</td>
	</tr>
	<tr>
		<td>分组信息</td>
			<td id="resourcePermissionTblTd">
				<#if groups?exists>
						<#list groups as group>
							<fieldset id="fsGroup${group_index?if_exists+1}">
							<input type="hidden" name="splitgp" value="gp">
							<legend>
								分组${group_index?if_exists+1}<input id="" name="filterGroupCheckBox" type="checkbox"
									value="${group_index?if_exists+1}">操作符
									<@eu.comboBox url="common.do?loadComboBox&key=RULE_OPTION&value=${(group.option)!}" name="groupOption"/>
									<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-add'" onclick="addFilterGroupRule(${group_index?if_exists+1});">新增规则</a> 
									<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-remove'" onclick="deleteFilterGroupRule(${group_index?if_exists+1});">删除规则</a>
							</legend>
							<div id="filterGroupRules${group_index?if_exists+1}">
								<#if (group.rules)?exists>
									<#list group.rules as rule>
										<div class="filterGroupRules"  id="filterGroupRuleDiv${group_index?if_exists+1}${rule_index?if_exists+1}">规则${rule_index?if_exists+1}<input type="hidden" name="splitrl" value="rl"><input
												id="filterRuleCheckbox" name="filterGroupRuleCheckBox"
												type="checkbox" value="${rule_index?if_exists+1}">
												<@eu.comboBox  url="resource.do?getSelectFieldsByTableName&value=${(rule.field)!}&tableName=${tableName!}" name="ruleField"/>
												&nbsp;&nbsp;<@eu.comboBox  url="common.do?loadComboBox&key=RULE_TYPE&value=${(rule.type)!}" name="ruleType"/>
												&nbsp;&nbsp;<@eu.comboTree url="resource.do?loadRuleValues&fieldValue=${(rule.field)!}&value=${(rule.value)!}" name="ruleValue" multiple="true"/>
												<#if (rule.option)?exists>
												&nbsp;&nbsp;<@eu.comboBox url="common.do?loadComboBox&key=RULE_OPTION&value=${(rule.option)!}" name="ruleOption"/>
												</#if>
										</div>
									</#list>
								</#if>
							</div>
						</fieldset>
						</#list>
					</#if>
			</td>
		</td>
	</tr>
</table>
</form>



